﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Runtime.Serialization.Json;

namespace EGov
{
    public partial class dangnhap : PhoneApplicationPage
    {
        int userid = 0;

        public dangnhap()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            string username = txtUsername.Text;
            string pass = pwdPass.Password;
            if (username != "" && pass != "")
            {
                string link = "http://egov047.juddy.vn/api/Accounts/login?userName=" + username.Trim() + "&pass=" + pass.Trim();
                WebClient client = new WebClient();
                client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
                client.OpenReadAsync(new Uri(link), UriKind.Absolute);
            }
            else
            {
                MessageBox.Show("Vui long nhap ten dang nhap va mat khau!");
            }
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            try
            {
                var ser = new DataContractJsonSerializer(typeof(int), null);
                userid = (int)ser.ReadObject(e.Result);
                MessageBox.Show(userid.ToString());
                if (userid != 0)
                {
                    NavigationService.Navigate(new Uri("/tinmoi.xaml", UriKind.Relative));
                }
                else
                {
                    MessageBox.Show("Tai khoan va mat khau khong dung. Vui long kiem tra lai!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        

    }
}